const { whisper, warn, info } = require("../utils/log_tool");
const { git_is_dirty, git_commit, git_push } = require("../utils/scm_tool");
const { toArray } = require("../utils/transform_tool");
const { BaseCommand } = require("./base");

class GitCommitCommand extends BaseCommand {
    async execute() {
        const data = this.selfData;
        if (!data.force) {
            const is_dirty = await git_is_dirty(data.cwd);
            if (!is_dirty) {
                whisper(
                    `没有要git提交的更改 ${data.cwd} ${data.msg}`,
                    undefined,
                    true,
                );
            } else {
                info(`准备git提交 ${data.cwd} ${data.msg}`);
                await git_commit(data.cwd, data.msg);
            }
        }
        let pushCfgs = data.push;
        if (pushCfgs) {
            pushCfgs = toArray(pushCfgs);
            for (let i = 0; i < pushCfgs.length; i++) {
                const pushCfg = pushCfgs[i];
                await git_push(data.cwd, pushCfg);
            }
        }
    }
}

module.exports = {
    GitCommitCommand,
};
